package com.music.service.songs.beans;

import java.util.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.music.service.songs.constants.SongState;
import com.music.service.urls.beans.Url;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Song {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long songId;
	@Persistent
	private String name;
	@Persistent
	private Long composerId;
	@Persistent
	private Long preferredUrlId;
	@Persistent
	private int state;
	@Persistent
	private Long createdMemberId;
	
	@Persistent
	private Date creationDate;
	@Persistent
	private Date lastUpdateDate;
	
	/** Preferred url for this song**/
	@NotPersistent
	private Url preferredUrl;
	
	/** Member specific url **/
	@NotPersistent
	private Url url;
	
	/** A map of all urls for this song with url id **/
	@NotPersistent
	private Map<Long, Url> urlsMap;
	
	
	public Song(){
		urlsMap = new HashMap<Long, Url>();
		state = SongState.ACTIVE.getState();
		
	}
	public Song(Long songId, String name, Long composerId, Long preferredUrlId,
			SongState songState, Long createdMemberId, Date creationDate,
			Date lastUpdateDate) {
		this.songId = songId;
		this.name = name;
		this.composerId = composerId;
		this.preferredUrlId = preferredUrlId;
		this.state = songState.getState();
		this.createdMemberId = createdMemberId;
		this.creationDate = creationDate;
		this.lastUpdateDate = lastUpdateDate;
	}
	
	public void setSongId(Long songId) {
		this.songId = songId;
	}
	public Long getSongId() {
		return songId;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setComposerId(Long composerId) {
		this.composerId = composerId;
	}
	public Long getComposerId() {
		return composerId;
	}
	public void setPreferredUrlId(Long preferredUrlId) {
		this.preferredUrlId = preferredUrlId;
	}
	public Long getPreferredUrlId() {
		return preferredUrlId;
	}
	public void setSongState(SongState songState) {
		this.state = songState.getState();
	}
	public SongState getSongState() {
		return SongState.getState(state);
	}
	public void setCreatedMemberId(Long createdMemberId) {
		this.createdMemberId = createdMemberId;
	}
	public Long getCreatedMemberId() {
		return createdMemberId;
	}
	public void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}
	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	


	/**
	 * @return the preferredUrl
	 */
	public Url getPreferredUrl() {
		return preferredUrl;
	}
	/**
	 * @param pPreferredUrl the preferredUrl to set
	 */
	public void setPreferredUrl(Url pPreferredUrl) {
		preferredUrl = pPreferredUrl;
	}
	/**
	 * @return the url
	 */
	public Url getUrl() {
		return url;
	}
	/**
	 * @param pUrl the url to set
	 */
	public void setUrl(Url pUrl) {
		url = pUrl;
	}
	/**
	 * @return the urlsMap
	 */
	public Map<Long, Url> getUrlsMap() {
		return urlsMap;
	}
	/**
	 * @param pUrlsMap the urlsMap to set
	 */
	public void setUrlsMap(Map<Long, Url> pUrlsMap) {
		urlsMap = pUrlsMap;
	}
	
	public Collection<Url> getAllUrls(){
		if(getUrlsMap() != null){
			return getUrlsMap().values();
		}
		//return empty array.
		return new ArrayList<Url>();
	}
	
	public Collection<Long> getAllUrlIds(){
		if(getUrlsMap() != null){
			return getUrlsMap().keySet();
		}
		//return empty array
		return new HashSet<Long>();
	}
	
	public void addUrl(Url pUrl){
		if(getUrlsMap() == null){
			setUrlsMap(new HashMap<Long, Url>());
		}
		getUrlsMap().put(pUrl.getUrlId(),pUrl);
	}
}
